var gwpz = {
    setBtnStatus: function () {
        var row = $("#gwpz_grid").datagrid("getSelected");
        if(row){
            $("#btn_gwpz_del").show();
        } else{
            $("#btn_gwpz_del").hide();
        }
    }
};

$(function () {
    //表格初始加载
    $('#gwpz_grid').datagrid({
        url: $.webPath() + "/sys/gwpz/gwpzList",
        toolbar: "#gwpz_toolbar",
        fitColumns: true,
        singleSelect: true,
        pagination: true,
        fit: true,
        pageNumber: 1,
        rownumbers: true,
        striped: true,
        columns: [
            [
                {field: 'id', title: '岗位ID',hidden:true},
                {field: 'mc', title: '岗位名称', align: 'left', width: 8},
                {field: 'jsList', title: '包含角色', align: 'left', width: 40}
            ]
        ],
        onLoadSuccess:function () {
            gwpz.setBtnStatus();
        },
        onClickRow: function () {
            gwpz.setBtnStatus();
        }
    });
    //**********按钮触发事件**********
    //新增按钮
    $("#btn_gwpz_add").click(function () {
        gwpz_add('新增岗位配置', 'add_gwpz');
    });
    //删除按钮
    $("#btn_gwpz_del").click(function () {
        gwpz_delete();
    });

    //var isFirst = true;
    //加载公司机构树
    $("#gwpz_tree").tree({
        loadFilter: function (rows) {
            return convert(rows);
        },
        onLoadSuccess: function () {
            var rootNode = $("#gwpz_tree").tree("getRoot");
            rootNode.target.click();
            searchGwpz();
//            if (isFirst) {
//                setTimeout(function () {
//                    searchGwpz();
//                }, 300)
//            } else {
//                isFirst = true;
//            }
        },
        onClick: function () {
            searchGwpz();
        }
    });
});

//弹出新增岗位配置页面
function gwpz_add(title, action) {
    var gsjgId = $("#gwpz_tree").tree("getSelected") == null ? "" : $("#gwpz_tree").tree("getSelected").id;
    var href = $.webPath() + "/sys/gwpz/gwpz_content?gsjgId=" + gsjgId;
    var actionUrl = $.webPath() + "/sys/gwpz/" + action;
    showGwpzDialog(title, href, actionUrl);
}

//弹出框的页面展示
function showGwpzDialog(title, href, actionUrl) {
    var win = $.window({
        onBeforeClose: function () {
            $(".validatebox-tip").remove();
        },
        title: title,
        width: 800,
        height: 400,
        cache: false,
        url: href,
        modal: true,
        buttons: [
            {
                text: '保存',
                handler: function () {
                    $(win).mask();
                    $("#gwpzForm").form('submit', {
                        url: actionUrl,
                        onSubmit: function (param) {
                            var isValid = $(this).form('validate');
                            if (!isValid) {
                                $(win).mask('hide');
                            }
                            var data = $('#gwpz_gwmx').datagrid('getChecked');
                            if(data.length==0 ){
                                $(win).mask('hide');
                                $.messager.alert("提示", "请选择需要配置的岗位!", "waring");
                                return false;
                            }
                            var arrayPrefix = 'gsjg.gsjggwList';
                            var paramString = "{";
                            for (var i = 0; i < data.length; i++) {
                                var objectString = $.toJSONStringSubPrefix(data[i], arrayPrefix + "[" + i + "].", [
                                    ['id', 'id']
                                ]);
                                paramString = paramString + objectString.substring(1, objectString.length - 1) + ",";
                            } ;
                            if (data && data.length > 0)
                                paramString = paramString.substring(0, paramString.length - 1);
                            paramString += "}";
                            $.extend(param, $.parseJSON(paramString));
                            return isValid;
                        },
                        success: function (data) {
                            $(win).mask('hide');
                            var json = $.parseJSON(data);
                            if (json.statusCode == 200) {
                                $(win).window('close');
                                searchGwpz();
                                $.messager.show({
                                    title: '提示框',
                                    msg: json.message,
                                    timeout: 3000,
                                    showType: 'slide'
                                });
                            } else {
                                $.messager.alert('提示', json.message, 'error');
                            }
                        }
                    });
                }
            }
        ]
    });
}

//删除岗位配置的方法
function gwpz_delete() {
    var record = $("#gwpz_grid").datagrid("getSelected");
    if (record == null) {
        $.messager.alert("提示", "请选择要删除的记录!", "info");
        return;
    }
    $.messager.confirm("提示", "是否确认删除该岗位配置?", function (r) {
        if (r) {
            $.ajax({
                type: "POST",
                url: $.webPath() + "/sys/gwpz/delete_gwpz",
                dataType: "json",
                data: {gwId: record.id,gsjgId : $("#gwpz_tree").tree("getSelected") == null ? "" : $("#gwpz_tree").tree("getSelected").id},
                beforeSend: function () {
                    $("#gwpz_layout").mask();
                },
                success: function (data) {
                    $("#gwpz_layout").mask('hide');
                    var icon = "info";
                    if (data.statusCode == 300)
                        icon = "error";
                    $.messager.alert("提示", data.message, icon);
                    if (data.statusCode == 200)
                        searchGwpz();
                }
            });
        }
    });
}

//查询岗位配置
function searchGwpz() {
    var isSelected = $("#gwpz_tree").tree("getSelected");
    var selectedNodeID;
    if (isSelected) {
        if ($("#gwpz_tree").tree("getSelected").attributes &&
            $("#gwpz_tree").tree("getSelected").attributes.parentId != null) {
            selectedNodeID = $("#gwpz_tree").tree("getSelected").id;
        }else{
            selectedNodeID = $("#gwpz_tree").tree("getRoot").id
        }
    }
    $('#gwpz_grid').datagrid({
        url: $.webPath() + "/sys/gwpz/gwpzList",
        pageNumber: 1,
        queryParams: {gsjgId: selectedNodeID}
    });
}